﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Awesomium.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Drawing;
using System.Threading;

using Awesomium.Core;

namespace Winz.FoeBot
{
    public static class Extensions
    {


        public static void Save(this List<MotivationPreference> list)
        {
            TextWriter writer = null;
            string path = Bot.settings.fileMotivationPrefs;
            XmlSerializer ser = new XmlSerializer(typeof(List<MotivationPreference>));
            try
            {
                writer = new StreamWriter(path);
            }
            catch (DirectoryNotFoundException e)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
                writer = new StreamWriter(path);
            }
            finally {
                using (writer)
                {
                    ser.Serialize(writer, list);
                    writer.Close();
                }
            }

        }

        public static List<MotivationPreference> Load(this List<MotivationPreference> list)
        {
            XmlSerializer ser = null;
            try
            {
                ser = new XmlSerializer(typeof(List<MotivationPreference>));
            }
            catch (Exception e)
            {
            }
            TextReader reader = null;
            string path = Bot.settings.fileMotivationPrefs;
            try
            {
                reader = new StreamReader(path);
                list = (List<MotivationPreference>)ser.Deserialize(reader);
                reader.Close();
            }
            catch (FileNotFoundException e)
            {
                list = new List<MotivationPreference>();
            }
            catch (DirectoryNotFoundException e)
            {
                list = new List<MotivationPreference>();
            }

            return list;
        }

        public static void InvokeIfRequired<T>(this T c, Action<T> action)
               where T : Control
        {
            if (c.InvokeRequired)
            {
                c.Invoke(new Action(() => action(c)));
            }
            else
            {
                action(c);
            }
        }

        public static void SetBotMode(this WebControl browser, bool isBotMode)
        {
            browser.InvokeIfRequired(b =>
            {
                if (isBotMode)
                {
                    browser.Dock = DockStyle.None;
                    browser.Width = 4000;
                    browser.Height = 2000;
                    ((Panel)browser.Parent).ScrollToBottom();
                }
                else
                    browser.Dock = DockStyle.Fill;
            });
        }



        public static bool FoeIsZoomedOut(this IWebView browser)
        {
            string path = @"img\zin.bmp";

            Point p = new Point(187, browser.Height - 21);
            Size s = new Size(10, 10);
            
            Rectangle cropRect = new Rectangle(p,s);
            Image src = ((ImageSurface)browser.Surface).Image as Image;
            Image zoomBtn = new Bitmap(cropRect.Width, cropRect.Height);
            using (Graphics g = Graphics.FromImage(zoomBtn))
            {
                g.DrawImage(src, new Rectangle(0, 0, zoomBtn.Width, zoomBtn.Height), 
                                cropRect,                        
                                GraphicsUnit.Pixel);
            }


            if (!File.Exists(path))
                zoomBtn.Save(path);


            Bitmap zoomIn = (Bitmap)Bitmap.FromFile(path);

            return zoomBtn.IsEqual(zoomIn);
        }


        public static bool FoeIsZoomedIn(this IWebView browser)
        {
            string path = @"img\zout.bmp";


            Point p = new Point(187, browser.Height - 21);
            Size s = new Size(10, 10);

            Rectangle cropRect = new Rectangle(p, s);
            Image src = ((ImageSurface)browser.Surface).Image as Image;
            Image zoomBtn = new Bitmap(cropRect.Width, cropRect.Height);
            using (Graphics g = Graphics.FromImage(zoomBtn))
            {
                g.DrawImage(src, new Rectangle(0, 0, zoomBtn.Width, zoomBtn.Height),
                                cropRect,
                                GraphicsUnit.Pixel);
            }


            if (!File.Exists(path))
                zoomBtn.Save(path);


            Bitmap zoomOut = (Bitmap)Bitmap.FromFile(path);
            return zoomBtn.IsEqual(zoomOut);
        }



        public static void FoeZoomIn(this WebControl browser)
        {
            if (browser.FoeIsZoomedOut())
                browser.LeftClick(193, browser.Height - 15);
        }

        public static void FoeZoomOut(this WebControl browser)
        {
            if (browser.FoeIsZoomedIn())
                browser.LeftClick(193, browser.Height - 15);
        }
        
        public static void TakeScreenshot(this WebControl browser) {
            string path;

            Bitmap bitmap = new Bitmap(browser.Width, browser.Height);
            Rectangle bitmapRect = new Rectangle(0, 0, browser.Width, browser.Height);
            browser.DrawToBitmap(bitmap, bitmapRect);
            
            int i = 1;
            path = Bot.settings.folderScreenShots + @"\screenshot" + i.ToString() + ".bmp";
            while (File.Exists(path))
            {
                i++;
                path = Bot.settings.folderScreenShots + @"\screenshot" + i.ToString() + ".bmp";
            }

            if(!Directory.Exists(Path.GetDirectoryName(path)))
                Directory.CreateDirectory(Path.GetDirectoryName(path));

            bitmap.Save(path);
        }




        public static void ScrollToBottom(this Panel p)
        {
            using (Control c = new Control() { Parent = p, Dock = DockStyle.Bottom })
            {
                p.ScrollControlIntoView(c);
                c.Parent = null;
            }
        }


        public static void LeftClick(this Awesomium.Windows.Forms.WebControl webControl, int x, int y)
        {
            webControl.InvokeIfRequired((ctl) =>
            {
                ((Awesomium.Core.IWebView)ctl)._leftClick(x, y);
            });
            
        }

        
        private static void _leftClick(this Awesomium.Core.IWebView webView, int x, int y)
        {
            
            webView.InjectMouseMove(x, y);
            webView.InjectMouseDown(Awesomium.Core.MouseButton.Left);
            webView.InjectMouseUp(Awesomium.Core.MouseButton.Left);
        }
        


    }



}
